<template>
  <div class="app-container">
    <el-row>
      <el-col :span="24" v-show="showTable">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px" @submit.native.prevent>
          <el-form-item label="文件名称" prop="name">
            <el-input
              v-model="queryParams.name"
              placeholder="请输入文件名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          </el-form-item>
        </el-form>
        <el-table v-loading="loading" :data="fileInfoList" @selection-change="handleSelectionChange" @row-click="selectRow">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="文件类型" align="center" prop="name" >
            <template slot-scope="scope">
              <svg-icon :icon-class="getIcon(scope.row.type)" style="font-size: 32px"/>
            </template>
          </el-table-column>
          <el-table-column label="文件名称" align="center" prop="name" />
          <el-table-column label="文件后缀" align="center" prop="type" />
          <el-table-column label="文件大小" align="center" prop="size" />
          <el-table-column label="创建人" align="center" prop="createBy" />
          <el-table-column label="创建时间" align="center" prop="createTime" />
          <el-table-column label="备注" align="center" prop="remark" />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-download"
                v-if="scope.row.type != 'folder'"
                @click="handleDownload(scope.row)"
              >下载</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-zoom-in"
                v-if="scope.row.type != 'folder'"
                @click="handlePreview(scope.row)"
              >预览</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
      <el-col :span="12" v-show="!showTable" :offset="6">
        <el-input placeholder="请输入文件名称" v-model="queryParams.name" class="searchBar" @keyup.enter.native="handleQuery" size="large">
          <el-button slot="append" icon="el-icon-search" @click="handleQuery"></el-button>
        </el-input>
      </el-col>
    </el-row>

    <el-dialog title="文件预览" :visible.sync="openPreview" v-if="openPreview" width="1300px" append-to-body>
      <file-preview :src="fileUrl"></file-preview>
    </el-dialog>
  </div>
</template>

<script>
import {
  queryMyFileInfo,
  getFileInfo,
  delFileInfo,
  addFileInfo,
  queryFileInfo,
  setFileInfoView
} from "@/api/fileInfo/fileInfo";
import { addFolder, myTreeselectFileInfo, updateFolder } from '../../../api/fileInfo/fileInfo'
import FilePreview from "@/components/FilePreview/index"


export default {
  name: "FileInfo",
  components:{FilePreview},
  data() {
    return {
      // 遮罩层
      loading: false,
      openPreview:false,
      fileUrl:"",
      showTable:false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 文件表格数据
      fileInfoList: [],
      defaultProps: {
        children: "children",
        label: "label"
      },
      folderName:"",
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openFolder:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ancestors: null,
        name: null,
        type: null,
        url: null,
        size: null
      },
      // 表单参数
      form: {},
      formFolder:{},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    //this.getList();
  },
  methods: {
    selectRow(row){
      if (row.type == "folder"){
        this.queryParams.parentId = row.id;
        this.queryParams.name = null;
        this.getList();
      }
    },
    /** 查询文件列表 */
    getList() {
      function sortItems(items) {
        return items.sort((a, b) => {
          if (a.type === 'folder' && b.type !== 'folder') {
            return -1;
          }
          if (a.type !== 'folder' && b.type === 'folder') {
            return 1;
          }
          return a.seqNo - b.seqNo;
        });
      }
/*      if (!this.queryParams.name){
        this.$modal.msgError("请输入查询的文件名");
        return;
      }*/
      this.loading = true;
      console.log(this.$route.path)
      if (this.$route.path.indexOf("fileInfoSearchAdmin")>-1) {
        queryFileInfo(this.queryParams).then(response => {
          let fileInfoList = response.data;
          this.fileInfoList = sortItems(fileInfoList)
          this.total = response.total;
          this.loading = false;
        });
      }else {
        queryMyFileInfo(this.queryParams).then(response => {
          let fileInfoList = response.data;
          this.fileInfoList = sortItems(fileInfoList)
          this.total = response.total;
          this.loading = false;
        });
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.showTable = true
      this.queryParams.pageNum = 1;
      this.queryParams.parentId = null;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    handleDownload(row){
      window.open(process.env.VUE_APP_BASE_API+row.url)
    },
    handlePreview(row){
      let type = row.type;
      setFileInfoView({id:row.id})
      if (type == "pdf"){
        window.open(process.env.VUE_APP_BASE_API+row.url)
      }else {
        this.openPreview = true;
        this.fileUrl = process.env.VUE_APP_BASE_API+row.url;
      }
    },
  }
};
</script>
<style lang="scss">
.searchBar{
  margin-top: 200px;
}

</style>
